@extends('layout.master')

@section('title','健康资讯')

@section('css')
    @parent
    <link href="{{ asset('plugins/bower_components/summernote/dist/summernote.css') }}" rel="stylesheet"/>
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
    <style>
        .comment {
            word-break: normal;
            white-space: pre-wrap;
        }
    </style>
@endsection
@section('main')
    <div class="white-box">
        <div class="row">
            <div class="col-md-9 col-sm-9">
                <span style="font-size: 23px;color: #000;font-weight: bolder">{{ $healthAdvice->title }}</span>
            </div>

            <div class="col-md-3 col-sm-3">
                <div class="row">
                    <div class="col-md-12 col-sm-12">
                        <span style="font-size: small">发布者：{{ $healthAdvice->user_name }}</span>
                    </div>
                    <div class="col-md-12 col-sm-12">
                        <audio controls="controls">
                            <source src="{{ $healthAdvice->audio_path }}" type="audio/mp3"/>
                            <source src="{{ $healthAdvice->audio_path }}" type="audio/ogg"/>
                        </audio>
                    </div>
                </div>

            </div>
        </div>

        <div class="row">
            <input type="hidden" id="content" value="{{ $healthAdvice->content }}">
            <div class="col-md-12 col-sm-12 content">
            </div>
        </div>

        <div class="form-group">
            <a class="fcbtn btn btn-info btn-outline btn-1e waves-effect" href="{{ url('/healthAdvice/list') }}">返回
            </a>
        </div>

        {{--展示回复--}}
        <div class="table-responsive">
            <table id="example23" class="display nowrap" cellspacing="0" width="100%"
                   data-url="{{ url(request()->getUri()) }}">
                <thead>
                <tr>
                    <th>创建人</th>
                    <th>头像</th>
                    <th>评论</th>
                    <th>发表时间</th>
                </tr>
                </thead>
                <tbody id="repays">
                </tbody>
            </table>
        </div>
    </div>

    {{--回复--}}
    <div class="white-box">
        <form class="form-horizontal form-material" method="post" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-12 col-sm-12">
                    <input type="text" name="contents" id="contents"
                           class="form-control form-control-line" required>
                </div>
            </div>
            <br/>
            <div class="row">
                <div class="col-md-4 col-sm-4">
                    <div class="example">
                        <select id="user" name="user">
                            @foreach(App\User::PostingsUsers() as $value)
                                <option value="{{ $value->c_id }}">{{ $value->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="col-md-2 col-sm-4">
                    <button class="fcbtn btn btn-success btn-outline btn-1e waves-effect" id="repay">回复
                    </button>
                </div>
            </div>
        </form>
    </div>


@endsection
@section('js')
    @parent
    <script src="{{ asset('js/myself.js') }}"></script>
    <script>
        $(document).ready(function () {
            $('.content').html($("#content").val());
        });

        $("#repay").click(function (e) {
            e.preventDefault();
            var information_id = geturlid();
            var user_id = $("#user").val();
            var contents = $("#contents").val();
            swal({
                    title: "",
                    text: "确定回复么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/healthAdvice/repays",
                        type: "POST",
                        data: {
                            information_id: information_id,
                            user_id: user_id,
                            contents: contents
                        },
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);
                            } else {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    // $("#repays").append(" <hr/><div class='row'>" +
                                    //     "<div class='col-md-1 col-sm-1'>" +
                                    //     "<img src='" + data.data.user_head + "' width='100%'>" +
                                    //     "</div>" +
                                    //     "<div class='col-md-1 col-sm-1'>" +
                                    //     "<span style='font-weight: bold;font-size: large'>" + data.data.user_name + "</span>" +
                                    //     "<span style='color: lightgrey;'>" + data.data.creat_time + "</span>" +
                                    //     "</div>" +
                                    //     "<div class='col-md-10 col-sm-10'>" +
                                    //     "<span>" + data.data.contents + "</span>" +
                                    //     "</div>" +
                                    //     "</div>");
                                    $("#repays").append("<tr role='row' class='even'>" +
                                        "<td>" + data.data.user_name + "</td>" +
                                        "<td><img src='" + data.data.user_head + "' width='100px'></td>" +
                                        "<td><p class='comment'>" + data.data.contents + "</p></td>" +
                                        "<td>" + data.data.creat_time + "</td>" +
                                        "</tr>")
                                });
                            }
                        }
                    })
                }
            );
        })
    </script>
@endsection